/*
 * Menu: Actionscript > Generate String Const
 * Key: M3+3
 * DOM: http://download.eclipse.org/technology/dash/update/org.eclipse.eclipsemonkey.lang.javascript
 * Author: Igor Wojda (Panel)
 * Homepage: http://www.panellabs.voyda.eu
 * Version 1.0
 */
 
function main()
{
	var editor = editors.activeEditor;
	var source = editor.source;

	if (editor.selectionRange)
	{
		var range = editor.selectionRange;
		text =  getText(editor);
		var constsData = getConstsData(text);
		
		if (constsData.length > 0)
		{
			var tabulators = getTabulators(getNUmberOfTabulators(editor));
			
			var constsCode = "";
			for(var i=0; i<constsData.length; i++)
			{
				constsCode += (tabulators + getConstCode(constsData[i]));
				if(i < constsData.length - 1)
					constsCode += "\n";
			}
			
			editor.applyEdit(getLinesBeginOffset(editor), getRangeLength(editor), constsCode);
		}
		else
			alert(" Generate String Const script input error! \n\nYou have to select variable name or put cursor in line containing name. Script is adapted for uppercase names ex. \n\n UPPERCASE_NAME \n");
   } 
}

function getRangeLength(editor)
{
	return getLinesEndOffset(editor) - getLinesBeginOffset(editor)
}

function getTabulators(number)
{
	var tabulators = "";
	
	for(var i=0; i<number; i++)
	{
		tabulators += "\t";
	}
	
	return tabulators;
}

function getNUmberOfTabulators(editor)
{
	var tabulatorCharCode = "9";
	var beginOffsett = getLinesBeginOffset(editor);
	numberOfTabulators = 0;
	
	for(var i=beginOffsett; i<editor.source.length; i++)
	{
		if(editor.source.charCodeAt(i) == tabulatorCharCode)
			numberOfTabulators++;
		else
			break;
	}
	
	return numberOfTabulators;
}

function getConstCode(str)
{
	code = 'public static const ' + str + ':String = "' + getValueFromActionscriptConst(str) + '";';
	return code;
}

function getConstsData(str)
{
	var results = str.match(/\w+/g);
	
	if(results)
		return results;
	else
		return "";
}

function getText(editor)
{
	return editor.source.substring (getLinesBeginOffset(editor), getLinesEndOffset(editor));
}

//returns beginOffset of first selected line. If nothing is selected line with cursor is treated as one selected line
function getLinesBeginOffset(editor)
{
	var range = editor.selectionRange;
	var currentLineBeginOffset;
	
	var test = 0;
	
	if(isTextSelected(editor))
	{
		currentLineBeginOffset = range.startingOffset;
		
		for (var i = currentLineBeginOffset; i > 0; i--)
		{
			if(isNewLineCharacter(editor.source.charCodeAt(i)))
			{
				currentLineBeginOffset = i+1; //beginning of line
				break;
			}
		}
	}
	else
	{
		for (var i = editor.currentOffset-1; i > 0; i--) //if coursor is on the end of line char code==enterCharCode, so currentOffset-1 
		{
			if(isNewLineCharacter(editor.source.charCodeAt(i))) //beginning of line
			{
				currentLineBeginOffset = i+1;
				break;
			}
		}
	}

	return currentLineBeginOffset;
}

//returns beginOffset last selected line. If nothing is selected line with cursor is treated as one selected line
function getLinesEndOffset(editor)
{
	var range = editor.selectionRange;
	var currentLineEndOffset;
	
	if(range.startingOffset == range.endingOffset) //no text selected so return end line offset
	{
		for (var i = editor.currentOffset; i < editor.source.length; i++)
		{
			if(isNewLineCharacter(editor.source.charCodeAt(i)))
			{
				currentLineEndOffset = i;
				break;
			}
		}
	}
	else
	{
		currentLineEndOffset = range.endingOffset;
	}	
	
	return currentLineEndOffset;
}

function isNewLineCharacter(str)
{
	var enterCharCode = 13;
	var newLineCharCode = 10;
	
	if(str == enterCharCode || str ==  newLineCharCode)
		return true;
	else
		return false;
}

function isTextSelected(editor)
{
	var range = editor.selectionRange;
	
	if(range.startingOffset == range.endingOffset)
		return false;
	else
		return true;
}

function getValueFromActionscriptConst(str) //MOUSE_DOWN => MouseDown
{
	var name = "";
	var parts = str.split("_");
	
	for (var i = 0; i < parts.length; i++)
	{
		if(i > 0)
			parts[i] = firstChartoUpperCase(parts[i]);
		else
			parts[i] = parts[i].toLowerCase();
		
		name += parts[i];
	}

	return name;
}

function firstChartoUpperCase(str)
{
	if(str.length > 1)
	{
		str = str.toLowerCase();
	
		firstChar = str.charAt(0);
		firstChar = firstChar.toUpperCase();
		
		str = firstChar + str.substr(1, str.length);
	}
	else
		str = str.toUpperCase();
		
	return str;
}

function alert(str)
{
     Packages.org.eclipse.jface.dialogs.MessageDialog.openInformation(window.getShell(), "Monkey Dialog", str);
}